package com.wynn.dao;

import com.kenai.jaffl.annotations.In;
import com.wynn.pojo.Order;
import com.wynn.pojo.OrderException;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderDao {

    @Select("select count(*) from orders where status=#{s} AND QUARTER(createDate) = QUARTER(NOW())")
    int statisticByStatus(String s);

    @Select("select coalesce(sum(number),0) from orders where productNum = #{product} and date_format(createDate,'%Y-%m') = #{date}")
    int queryNumPidAndMonth(@Param("product") int id, @Param("date") String s);

    @Select("select count(*) from orders where QUARTER(createDate) = QUARTER(NOW())")
    int statisticAll();

    @Select("select * from orders")
    @Results({
        @Result(column = "productNum" , property = "product",
                one = @One(select = "com.wynn.dao.ProductDao.findProductById")
        )
    })
    List<Order> selectAll();

    @Select("select * from orders where id =#{id}")
    @Results({
            @Result(column = "productNum" , property = "product",
                    one = @One(select = "com.wynn.dao.ProductDao.findProductById")
            )
    })
    Order findById(int id);

    @Select({"<script>" ,
            "select * from orders",
            "<where>",
            "<if test='deadLine!=null'> Month(deadLine)=Month(#{deadLine})</if>",
            "<if test='company!=null'> and company=#{company}</if>",
            "<if test='product!=0'>and productNum=#{product}</if>",
            "</where>",
            "order by deadLine desc",
            "</script>"})
    @Results({
            @Result(column = "productNum" , property = "product",
            one = @One(select = "com.wynn.dao.ProductDao.findProductById"))
    })
    List<Order> searchAll(@Param("deadLine")Date deadLine,@Param("company")String company,@Param("product")Integer product);

    @Delete("delete from orders where id=#{id}")
    void deleteOrderById(int id);

    @Insert("insert into orders values(null,#{orderNum},#{product.id},#{number},#{createDate},#{deadLine},#{company},0)")
    void addOrder(Order order);

    @Update("update orders set number=#{number},createDate=#{createDate},deadLine=#{deadLine} where id=#{id}")
    void editOrder(Order order);

    @Update("update orders set status =#{s} where id=#{orderId}")
    void updateOrderStu(@Param("orderId") int orderId, @Param("s")String s);

    @Insert("insert into orderexception values(#{id},#{order.id},#{reportTime},#{content},0)")
    void addOrderException(OrderException exception);


    @Delete("delete from orderexception where orderId = #{order}")
    void deleteOrderExcByOid(int order);


    @Select("<script>" +
            "select * from orderexception oe,orders o " +
                "<where>" +
                    "oe.orderId = o.id" +
                    "<if test='deadLine!=null'> AND month(deadLine)=month(#{deadLine}) </if>" +
                    "<if test='company!=null'> AND company=#{company} </if>" +
                    "<if test='status!=-1'> AND oe.status=#{status} </if>" +
                "</where>" +
            "</script>")
    @Results({
            @Result(column = "orderNum" , property = "order.orderNum"),
            @Result(column = "company" , property = "order.company"),
            @Result(column = "deadLine" , property = "order.deadLine"),
    })
    List<OrderException> searchException(@Param("deadLine") Date deadLine, @Param("company") String company, @Param("status") int status);

    @Update("update orderexception set content=#{content} where id=#{id}")
    void editException(OrderException orderException);

    @Update("update orderexception set status=#{status} where id=#{id}")
    void updateOrderExceptionStu(@Param("id") int id, @Param("status")int i);
}
